package com.app.twth.ui.cyclecount;


import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityCycleCountBinding;
import com.app.twth.ui.cyclecount.location.CCLocationActivity;
import com.app.twth.ui.cyclecount.sku.CCSkuLPNActivity;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

/**
 * @describe :
 */
public class CycleCountActivity extends BaseActivity<ActivityCycleCountBinding, CycleCountViewModel> {
    private String TAG = getClass().getSimpleName();
    private int type = 0;


    @Override
    protected void initListener() {
        viewModel.cycleCountLocationQrLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", data);
            AppRouter.showActivityForResult(mContext, CCLocationActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.cycleCountSkuLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", data);
            AppRouter.showActivityForResult(mContext, CCSkuLPNActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return getString(R.string.Cyclecount);
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("");
        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);

        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_sku:
                        if (type == 0) {
                            return;
                        }
                        type = 0;
                        resetView(R.string.scan_sku_barcode_lower2);
                        break;
                    case R.id.rb_location:
                        if (type == 1) {
                            return;
                        }
                        type = 1;
                        resetView(R.string.scan_location_barcode_lower);
                        break;
                }

            }
        });

    }

    private void resetView(int code) {
        binding.rbSku.setChecked(type == 0 ? true : false);
        binding.rbLocation.setChecked(type == 1 ? true : false);
        binding.tvCode.setText("");
        binding.tvCode.setHint(getString(code));
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }


    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                bottomLayoutBinding.tvHint.setText("");
                binding.tvCode.setText("");
                break;
            case R.id.tv_next:
                barcodeStr = binding.tvCode.getText().toString();
                if (Utils.isEmpty(barcodeStr)) {
                    switch (type) {
                        case 0:
                            ToastUtil.showToast(R.string.scan_sku_barcode_lower2);
                            return;
                        case 1:
                            ToastUtil.showToast(R.string.scan_location_barcode_lower);
                            return;
                    }
                }
                getData();
                break;
        }
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                if (!Utils.isEmpty(barcodeStr)) {
                    getData();
                }

            }
            return false;
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        getData();
    }

    public void getData() {
        switch (type) {
            case 0:
                viewModel.cycleCountSku(barcodeStr);
                break;
            case 1:
                viewModel.cycleCountLocationInventory(barcodeStr, null);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            bottomLayoutBinding.tvHint.setText("");
            binding.tvCode.setText("");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
